package net.daum.webtoon.android.jibx.data;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.Calendar;

import android.content.Context;
import android.os.Environment;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.Toast;

public class FileManager {

	public static String TAG = FileManager.class.getSimpleName();

	public static String BASEPATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/";
	public static String WEEKLISTPATH = "/weekList/";
	public static String TOONSPATH = "/toons/";

	public static String makeFileNameByDate() {
		return DateFormat.format("yyyyMMdd", Calendar.getInstance().getTime()).toString();
	}

	public static String makeFileJpgName(String aUrl) {
		String[] returnStrs = aUrl.split("/");
		return returnStrs[returnStrs.length - 1] + ".jpg";
	}

	public static String makeFileName(String aUrl) {
		String[] returnStrs = aUrl.split("/");
		return returnStrs[returnStrs.length - 1];
	}

	public static void MakeToonFolder(Context context, String toonName) {
		try {
			ObjectOutputStream oos = null;
			String mSdPath = null;
			String ext = Environment.getExternalStorageState();
			if (ext.equals(Environment.MEDIA_MOUNTED)) {
				mSdPath = BASEPATH + context.getApplicationContext().getPackageName() + TOONSPATH + toonName;
			} else {
				mSdPath = Environment.MEDIA_UNMOUNTED;
			}
			File lDir = new File(mSdPath);
			if (!lDir.exists())
				lDir.mkdirs();
		} catch (Exception e) {

		}
	}

	public static void saveObj(Context context, Object aObj, String path, String fileName) {
		try {
			ObjectOutputStream oos = null;
			String mSdPath = null;
			String ext = Environment.getExternalStorageState();
			if (ext.equals(Environment.MEDIA_MOUNTED)) {
				mSdPath = BASEPATH + context.getApplicationContext().getPackageName() + path;
			} else {
				mSdPath = Environment.MEDIA_UNMOUNTED;
			}
			File lDir = new File(mSdPath);
			if (!lDir.exists())
				lDir.mkdirs();

			File file = new File(mSdPath, fileName);
			FileOutputStream fos = new FileOutputStream(file);
			oos = new ObjectOutputStream(new BufferedOutputStream(fos));
			oos.writeObject(aObj);
			oos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block  
			e.printStackTrace();
			Log.e(TAG, e.toString());
//			Toast.makeText(context, "fos IOException", Toast.LENGTH_SHORT).show();
		}
	}

	public static Object loadObj(Context context, String path, String fileName) {
		String mSdPath = null;
		String ext = Environment.getExternalStorageState();
		if (ext.equals(Environment.MEDIA_MOUNTED)) {
			mSdPath = BASEPATH + context.getApplicationContext().getPackageName() + path;
		} else {
			mSdPath = Environment.MEDIA_UNMOUNTED;
		}
//		File path = new File(mSdPath )
		File file = new File(mSdPath, fileName);
		if (!file.exists())
			return null;
		FileInputStream fis;
		Object simpleClass = null;

		try {
			fis = new FileInputStream(file);
			ObjectInputStream is = new ObjectInputStream(fis);
			simpleClass = (Object) is.readObject();
			is.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (StreamCorruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return simpleClass;
	}
}
